import java.security.PrivateKey;
import java.util.LinkedList;

public class ThreadPool {

    LinkedList<Runnable> tasks = new LinkedList<>();

    int threadCount;

    ThreadPool(){
        int threadCount = 10;
        synchronized (tasks){
            for(int i=0;i<threadCount;i++){
                new customThread("消费者线程"+i).start();
            }
        }
    }

    public void add(Runnable r ) {
       synchronized (tasks) {
           tasks.add(r);
           tasks.notifyAll();
       }
    }

    class customThread extends Thread{
        customThread(String name){
            super(name);
        }

        Runnable task;
        public void run(){
            System.out.println("启动： " + this.getName());
            while(true){

            synchronized (tasks) {
                while (tasks.isEmpty()) {
                    try {
                        tasks.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                task = tasks.removeLast();
                tasks.notifyAll();
            }
            System.out.println(this.getName()+"获取任务成功");
            task.run();
            }
        }
    }
}
